package com.study.equ;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Maps;
import com.study.equ.Demo.DataDTO.Equ;
import com.study.equ.Demo.DataDTO.Pass;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.CompareToBuilder;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2024/5/15 11:3
 */
public class Demo {


    public static Map<String, String> statusMap = Maps.newConcurrentMap();
    public static Map<String, String> passOpenMap = Maps.newConcurrentMap();
    public static Map<String, String> typeMap = Maps.newConcurrentMap();
    public static Map<String, String> inOutMap = Maps.newConcurrentMap();

    static {
        statusMap.put("100101", "正常");
        statusMap.put("100102", "停用");

        passOpenMap.put("101101", "通道开启");
        passOpenMap.put("101102", "通道关闭");

        typeMap.put("100515", "箱号识别");
        typeMap.put("100514", "IC卡读写器");
        typeMap.put("100509", "后地感");
        typeMap.put("100503", "地磅设备");
        typeMap.put("100502", "LED大屏");
        typeMap.put("100504", "报警器");
        typeMap.put("100506", "栏杆");
        typeMap.put("100507", "红外对射");
        typeMap.put("100501", "电子车牌");
        typeMap.put("100505", "红绿灯");
        typeMap.put("100508", "前地感");
        typeMap.put("100510", "车牌识别");
        typeMap.put("100511", "后栏杆");
        typeMap.put("100513", "后地感1");
        typeMap.put("100512", "前地感1");
        typeMap.put("100520", "串口服务器");
        typeMap.put("100521", "LED后屏");
        typeMap.put("100523", "引导屏");
        typeMap.put("100522", "核辐射");
        typeMap.put("100524", "闸机");

        inOutMap.put("100602", "出境");
        inOutMap.put("100601", "入境");
    }

    @Test
    public void equ() {
        String str1 = "";
        List<String> jsonList = FileUtil.readLines("/Users/zhuzi/Downloads/equ.json", StandardCharsets.UTF_8);


        List<List<Equ>> rows = jsonList.stream().filter(StringUtils::isNotBlank).map(str -> JSON.parseObject(str).getList("rows", Equ.class)).collect(Collectors.toList());
        List<Equ> equList = rows.stream().flatMap(List::stream).collect(Collectors.toList());

        equList.sort((a, b) -> new CompareToBuilder().append(a.getPassCode(), b.getPassCode()).append(a.getEqpType(), b.getEqpType()).toComparison());

        equList.forEach(e -> {
            e.setEqpType(typeMap.getOrDefault(e.getEqpType(), e.getEqpType()));

            e.setState(statusMap.getOrDefault(e.getState(), e.getState()));
            System.out.println(e);
        });

        EasyExcel.write("/Users/zhuzi/Downloads/sql/设备.xlsx", Equ.class).sheet("模板").doWrite(equList);


    }

    @Test
    public void pass() {

        String file = "/Users/zhuzi/Downloads/pass.json";
        List<String> jsonList = FileUtil.readLines(file, StandardCharsets.UTF_8);


        List<List<Pass>> rows = jsonList.stream().filter(StringUtils::isNotBlank).map(str -> JSON.parseObject(str).getList("rows", Pass.class)).collect(Collectors.toList());
        List<Pass> equList = rows.stream().flatMap(List::stream).sorted(Comparator.comparing(Pass::getPassCode)).collect(Collectors.toList());

        equList.forEach(p -> {
            p.setState(statusMap.getOrDefault(p.getState(), p.getState()));
            p.setEntryExitType(inOutMap.getOrDefault(p.getEntryExitType(), p.getEntryExitType()));
        });

        EasyExcel.write("通道1.xlsx", Pass.class).sheet("模板").doWrite(equList);
    }

    public interface DataDTO {

        @Data
        @Accessors(chain = true)
        @EqualsAndHashCode(of = "passCode")
        class Pass {
            @ExcelProperty("关去号")
            private String cusCode;
            @ExcelProperty("关区")
            private String cusName;

            @ExcelProperty("场站号")
            private String staCode;
            @ExcelProperty("场站名称")
            private String staName;


            @ExcelProperty("通道号")
            private String passCode;
            @ExcelProperty("通道名称")
            private String passName;

            @ExcelProperty("出入类型")
            private String entryExitType;

            @ExcelProperty("状态")
            private String state;
        }

        @ExcelIgnoreUnannotated
        @Data
        @Accessors(chain = true)
        class Equ {
            @ExcelProperty("关去号")
            private String cusCode;
            @ExcelProperty("关区")
            private String cusName;

            @ExcelProperty("场站号")
            private String staCode;
            @ExcelProperty("场站名称")
            private String staName;


            @ExcelProperty("通道号")
            private String passCode;
            @ExcelProperty("通道名称")
            private String passName;

            //@ExcelProperty("是否启用")
            private String state;


            //private String braName;

            @ExcelProperty("设备IP")
            private String eqpIp;
            //private String eqpName;
            @ExcelProperty("设备端口")
            private String eqpPort;
            @ExcelProperty("设备型号")

            private String eqpType;
            @ExcelProperty("驱动类")
            private String braCode;

            @ExcelProperty("驱动方法")
            private String fileCode;
            private String fileName;
            private String isActive;


        }
    }
}
